//
//  RootViewController.swift
//  sina----
//
//  Created by mac on 16/5/8.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class RootViewController: UIViewController {
    //下拉刷新
    let refreshControl: UIRefreshControl = UIRefreshControl()
    
    //上拉刷新
    let pushUpView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    
    var visitorView: VisitorView?
    let tableView: UITableView = UITableView()
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        setTableView()
        setVisitorView()
        //加载数据
        loadData()
    }
    
    
    
    private func setVisitorView() {
        visitorView = VisitorView()
        visitorView?.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        view = visitorView
    }
    private func setTableView() {
        view = tableView
        tableView.dataSource = self
        tableView.delegate = self
    }
    
    func setRefresh() {
        //设置下拉刷新
        tableView.addSubview(refreshControl)
        refreshControl.addTarget(self, action: "pushDown", forControlEvents: UIControlEvents.ValueChanged)
        //上拉刷新
        tableView.tableFooterView = pushUpView
    }
    
    func pushDown() {
        print("下拉刷新")
        loadData()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    
    func loadData() {
        
    }

}
// MARK: - 数据源, 代理方法
extension RootViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        return UITableViewCell()
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let section = tableView.numberOfSections - 1
        let row = tableView.numberOfRowsInSection(indexPath.section) - 1
        
        if section == indexPath.section && row == indexPath.row {
            
            print("上拉刷新")
            pushUpView.startAnimating()
            
            loadData()
        }
    }
}